<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\order\event;
use think\Db;
use think\facade\Log;
/**
 *  取消订单钩子
 *  @author hu
 */
class CancelOrder {
    
    
    
  
    
    /**
     * 批量自动取消订单
     * @author 晓风
     * @return int
     */
    public function run($playload = []){        
        $timeout = module_config('order.order_timeout') * 60;
        $order_map[] = ['status', '=', 0];
        $order_map[] = ['create_time', '<', (time() - $timeout)];
        $orders = \app\common\model\Order::where($order_map)->select();        
        foreach ($orders as $order) {
            $this->cancelOrder($order);  
        }
        return 1;
     }      
     //自动取消订单 ,以下根据你的实际业务填写 
     protected function cancelOrder($order){
        Db::startTrans();
        try{
           $type = "";
           switch($order["order_type"]){
                case 1:
                case 2:    
                    \app\common\model\Order::where("order_sn",$order["order_sn"])->delete();//充值订单物理删除
                    \app\order\model\OrderAction::where("order_sn",$order["order_sn"])->delete();//充值订单物理删除
                    $type = "充值";
                    break;             
                case 3:
                    \app\order\model\OrderGoods::cancelOrder($order);                        
                    $type = "商品";
                    break;         
                default:
                    \app\common\model\Order::where("order_sn",$order["order_sn"])->where("status",0)->update([
                        "cancel_time"=> time(),//存入取消时间，按照这个时间半小时判断status 是-2 就退库存
                        'status'=> -2
                    ]);
                    \app\order\model\OrderAction::addLog($order['order_sn'], "订单取消中", -2,0, '超时自动取消');
                  break;

            }
            if(!in_array($order["order_type"],[3])){
                \app\operation\model\SystemMessage::create([
                    "to_user_id"=> $order['user_id'], 
                    "order_sn"=> $order['order_sn'],                   
                    "title"=>'您的订单已超时取消',
                    "content"=> "您的".$type."订单：" . $order["order_sn"] . ",因超时已自动取消",
                    "msg_type"=>1,
                    "custom"=>json_encode(['type'=>'order','id'=> $order['order_sn']])
                ]);
            }
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();   
            Log::error($e->getMessage());            
        }      
    }   
    
      /**
     * 批量自动取消订单-退费结算
     * @author 晓风
     * @return int
     */
    public function refund($playload = []){        
        $timeout = 1800;
        $order_map[] = ['status', '=', -2];        
        $order_map[] = ['cancel_time', '<', (time() - $timeout)];
        $orders = \app\common\model\Order::where($order_map)->select();        
        foreach ($orders as $order) {
            $this->cancelOrderRefund($order);  
        }
        return 1;
     }      
    
     //自动取消订单-退款结算 ,以下根据你的实际业务填写 
     protected function cancelOrderRefund($order){
        Db::startTrans();
        try{         
           switch($order["order_type"]){      
                case 1:                 
                case 2:      
                    break;
                case 3:  
                    \app\order\model\OrderGoods::cancelOrderRefund($order);  
                    break;           
                default:
                    \app\common\model\Order::where("order_sn",$order["order_sn"])->where("status",-2)->setField('status',-1);
                    \app\order\model\OrderAction::addLog($order['order_sn'], "订单已取消", -1,0, '系统自动取消');
                  break;
            }         
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();   
            Log::error($e->getMessage());            
        }      
    }   
}
